<?
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

include("ext_lib/simple_html_dom.php");


foreach($html->find("a") as $element){
    if (str_endswith($element->href, ".flv")){
        include_once("utils/flvplayer.php");
        $flv_content = generate_flvplayer($element->href);
        $element->innertext = $flv_content;
        $element->href = "";
    }
    if ($element->class == "ekkin"){
        if ($element->id == "rss"){
            $source_lang = $element->hreflang;
            include_once("utils/rss.php");
            $rss_content = generate_rss($element->innertext, $items=5, $target_lang=$lang, $source_lang=$source_lang);
            $element->outertext = "<p>$rss_content</p>";
            //$element->href = "";
        }

    }
}

foreach($html->find("form") as $element){
    if ($element->class == "ekkin_mail"){
        $content = $element->innertext;
        $url = construct_link();
        foreach($element->find("input") as $element2){
            if ($element2->name == "form_return"){
                $page = $element2->value;
                $url = construct_link(array("page"))."page=$page";
            }
        }

        $div = $element->parent();
        $div->id = "form_mail2";
        $content .= "
        <input type=\"hidden\" value=\"$main_site_mail\" name=\"form_mail\"/>
        <input type=\"hidden\" value=\"$url\" name=\"form_url\"/>";
        //$element->action = "utils/mailform.php";
        $element->outertext = "<script type='text/javascript' src='../ext_lib/webtoolkit.aim.js'></script>
	<script type='text/javascript'>
function startCallback() {
    // make something useful before submit (onStart)
    return true;
}

function completeCallback(response) {
    // make something useful after (onComplete)

    document.getElementById('form_mail2').innerHTML = response;
}
</script>
<form class='ekkin_mail' action=\"utils/mailform.php\" method=\"POST\" onsubmit='return AIM.submit(this, {onStart : startCallback, onComplete : completeCallback})'>
$content
</form>";
    }
}

// Configure images for lightbox integration. when img id startswith lightbox, the values will be set in a hyperlink to trigger lightbox.
foreach($html->find("img") as $element){
    if (str_startswith($element->id, "lightbox")){
        $url = $element->src;
        $title = $element->title;
        $rel = $element->id;
        $thumb = str_replace("user_files/images", "user_files/images/thumbs", $url);
        $element->outertext = "<a href=\"$url\" title=\"$title\" rel=\"$rel\"><img src=\"$thumb\"/></a>";
    }
}

// Check ekkin special tags



$site_content = $html->save();

// replace special vars

$site_content = str_replace("%site_mail%", $main_site_mail, $site_content);
?>
